package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;


    @Override
    public void add(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        Long userId = BaseContext.getCurrentId();
        //判断当前菜品或套餐是否在购物车中
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        //所有用户的购物车都共享一个数据库表，查询时必须传递userId
        shoppingCart.setUserId(userId);
        //在查询条件为userId和setmealId或userId,dishId和dishFlavor下，list集合中要么没有要么只有一个
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        if (list != null && list.size() > 0) {
            //购物车中存在
            ShoppingCart cart = list.get(0);
            //更新购物车中当前物品的数量
            cart.setNumber(cart.getNumber() + 1);
            shoppingCartMapper.updateNumberById(cart);
        } else {
            //购物车中没有
            Long dishId = shoppingCartDTO.getDishId();
            Long setmealId = shoppingCartDTO.getSetmealId();
            if (dishId != null) {
                DishVO dish = dishMapper.getById(dishId);
                shoppingCart = ShoppingCart.builder()
                        .image(dish.getImage())
                        .name(dish.getName())
                        .userId(userId)
                        .dishId(dishId)
                        .createTime(LocalDateTime.now())
                        .dishFlavor(shoppingCartDTO.getDishFlavor())
                        .number(1)
                        .amount(dish.getPrice()).build();
            } else {
                SetmealVO setmeal = setmealMapper.getById(setmealId);
                shoppingCart = ShoppingCart.builder()
                        .image(setmeal.getImage())
                        .name(setmeal.getName())
                        .userId(userId)
                        .setmealId(setmealId)
                        .createTime(LocalDateTime.now())
                        .number(1)
                        .amount(setmeal.getPrice()).build();
            }
            shoppingCartMapper.add(shoppingCart);
        }

    }

    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(BaseContext.getCurrentId())
                .build();
        return shoppingCartMapper.list(shoppingCart);
    }

    @Override
    public void clean() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delete(shoppingCart);
    }

    @Override
    public void sub(ShoppingCartDTO shoppingCartDTO) {
        //1.先查到要减一的物品，看他在购物车中的数量是否为一
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        ShoppingCart cart = list.get(0);
        if(cart.getNumber()==1){
            //减完为0，则删除
            /*if(setmealId!=null){
                shoppingCart.setSetmealId(setmealId);
                shoppingCartMapper.delete(shoppingCart);
            }else{
                shoppingCart.setDishId(dishId);
                shoppingCart.setDishFlavor(dishFlavor);
                shoppingCartMapper.delete(shoppingCart);
            }*/
            shoppingCartMapper.delete(shoppingCart);
        }else{
            //number减一
            Integer number = cart.getNumber();
            cart.setNumber(number-1);
            shoppingCartMapper.updateNumberById(cart);
        }

    }
}
